import * as actionTypes from '../constants'

// 从localstorage中读取数据
let _user = JSON.parse(localStorage.getItem('user'))
let _token = localStorage.getItem('token')

const defaultState = {
  user: _user || {},
  token: _token || '',
  isLogin: _token && _user ? true : false
}

function reducer(state = defaultState, action) {
  const { type, data } = action
  switch (type) {
    case actionTypes.SAVE_USER_INFO:
      return { ...data, isLogin: true }
    case actionTypes.DELETE_USER_INFO:
      return { user: {}, token: '', isLogin: false }
    
    default:
      return state
  }
}

export default reducer








